package com.building.service.impl;

import com.building.dao.LiverecordDao;
import com.building.entity.DormitoryManager;
import com.building.entity.Liverecord;
import com.building.entity.Student;
import com.building.dao.StudentDao;
import com.building.response.ServerResponse;
import com.building.service.StudentService;
import com.building.sql.LiveRecordSql;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * (Student)表服务实现类
 *
 * @author TianCai
 * @since 2023-01-15 18:36:31
 */
@Service("studentService")
public class StudentServiceImpl implements StudentService {
    @Resource
    private StudentDao studentDao;
    @Resource
    private LiverecordDao liverecordDao;

    /**
     * 通过ID查询单条数据
     *
     * @param sId 主键
     * @return 实例对象
     */
    @Override
    public Student queryById(Integer sId) {
        return this.studentDao.queryById(sId);
    }

    /**
     * 分页查询
     *
     * @param student 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Student> queryByPage(Student student, PageRequest pageRequest) {
        long total = this.studentDao.count(student);
        return new PageImpl<>(this.studentDao.queryAllByLimit(student, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param student 实例对象
     * @return 实例对象
     */
    @Override
    public Student insert(Student student) {
        this.studentDao.insert(student);
        return student;
    }

    /**
     * 修改数据
     *
     * @param student 实例对象
     * @return 实例对象
     */
    @Override
    public Student update(Student student) {
        this.studentDao.update(student);
        return this.queryById(student.getSId());
    }

    /**
     * 通过主键删除数据
     *
     * @param sId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer sId) {
        return this.studentDao.deleteById(sId) > 0;
    }

    @Override
    public ServerResponse<PageInfo<Student>> getPageInfoByAccount(Integer pageNum, Integer pageSize, String account) {
        PageHelper.startPage(pageNum, pageSize);
        List<Student> students = this.studentDao.queryAllByAccount(account);
        PageInfo<Student> studentPageInfo = new PageInfo<>(students);
        int size = studentPageInfo.getList().size();
        if (size==0) return ServerResponse.createByErrorMessage("暂无数据!换个关键词试试吧!");
        return ServerResponse.createBySuccess("查询成功,共"+size+"条数据",studentPageInfo);
    }

    @Override
    public ServerResponse<PageInfo<Student>> getPageInfoByName(Integer pageNum, Integer pageSize, String name) {
        PageHelper.startPage(pageNum, pageSize);
        List<Student> students = this.studentDao.queryAllByName(name);
        PageInfo<Student> studentPageInfo = new PageInfo<>(students);
        int size = studentPageInfo.getList().size();
        if (size==0) return ServerResponse.createByErrorMessage("暂无数据!换个关键词试试吧!");
        return ServerResponse.createBySuccess("查询成功,共"+size+"条数据",studentPageInfo);
    }

    @Override
    public ServerResponse<PageInfo<Student>> getPageInfo(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Student> students = this.studentDao.queryAll();
        PageInfo<Student> studentPageInfo = new PageInfo<>(students);
        return ServerResponse.createBySuccess("获取分页数据成功!", studentPageInfo);
    }

    @Override
    public ServerResponse<Student> updateStudent(Integer id, String name, String number, String account, String password, Integer sex) {
        Student student = studentDao.queryById(id);
        if (student==null) return ServerResponse.createByErrorMessage("修改失败不存在这条数据");
        List<Student> students = studentDao.queryAll();
        int i = students.indexOf(student);//拿到这个学生的下标
        for (int count = 0;count< students.size();count++){
            if (count==i) continue;//把自己去掉之后看看有没有别的人和我用同样的账户名
            if (number.equals(students.get(count).getSNumber())) return ServerResponse.createByErrorMessage("此学生学号已经存在!");
            if (account.equals(students.get(count).getAccount())) return ServerResponse.createByErrorMessage("此账户名太受欢迎了换一个试试吧!");
        }
        student.setName(name);
        student.setSex(sex);
        student.setSNumber(number);
        student.setAccount(account);
        student.setPassword(password);
        student.setUpdateTime(new Date());
        student.setVersion(student.getVersion()+1);
        int update = studentDao.update(student);
        if (update!=1) return  ServerResponse.createByErrorMessage("数据修改失败,请检查服务器!");
        return ServerResponse.createBySuccess("修改数据成功!",student);
    }

    @Override
    public ServerResponse<Student> addStudent(String name, String number, String account, String password, Integer sex) {
        if (name.equals("") || account.equals("") || password.equals("")) return ServerResponse.createByErrorMessage("信息输入不完整!");
        if(studentDao.queryByAccount(account)!=null) return ServerResponse.createByErrorMessage("此账户名太受欢迎了换一个试试吧!");
        if(studentDao.queryAllByNumber(number)!=null) return ServerResponse.createByErrorMessage("此宿管编号已存在!");
        Student student = new Student(null,name,account,password,number,sex,new Date(),new Date(),1,true);
        int insert = studentDao.insert(student);
        if (insert!=1) return  ServerResponse.createByErrorMessage("数据添加失败,请检查服务器!");
        return ServerResponse.createBySuccess("添加数据成功!",student);
    }

    @Override
    public ServerResponse<Student> deleteStudent(Integer id) {
        Student student = this.studentDao.queryById(id);
        if (student == null) return ServerResponse.createByErrorMessage("此id对应的用户不存在");
        student.setStatus(false);
        student.setUpdateTime(new Date());
        student.setVersion(student.getVersion()+1);
        int update = studentDao.update(student);
        if (update!=1) return  ServerResponse.createByErrorMessage("数据删除失败,请检查服务器!");
        return ServerResponse.createBySuccess("删除用户成功!",student);
    }

    @Override
    public ServerResponse<List<Student>> getstudnetList() {
        CopyOnWriteArrayList<Student> list = new CopyOnWriteArrayList<>();
        List<Student> students = this.studentDao.queryAll();
        List<Liverecord> liverecords = liverecordDao.queryAllEntity();
        for (Student student : students) {
            list.add(student);
        }
        for (Student student :students){
            for (Liverecord liverecord: liverecords){
                if (student.getSId() == liverecord.getSId()){
                    //这里的意思是如果 记录中的学生id和现在学生id相同的话那就把这个学生从list集合中删除
                    list.remove(student);
                    //起到了一个过滤的效果，当然这个也可以在sql语句中直接写
                }
            }
        }
        if (list.size()==0) return ServerResponse.createByErrorMessage("当前学生已经全部入住");
        return  ServerResponse.createBySuccess("获取未入住学生信息成功!",list);
    }

    @Override
    public ServerResponse<List<Student>> getoldstudentList() {
        return ServerResponse.createBySuccess("获取未入住学生信息成功!",this.studentDao.queryAll());
    }

    @Override
    public ServerResponse<Integer> studentListCount() {
        return ServerResponse.createBySuccess("系统学生总人数!",this.studentDao.queryAllCount());
    }
}